﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace RailwayReservationWeb.manager
{
    public partial class schedule_edit : System.Web.UI.Page
    {
        private RailwayReservationDataContext db;

        protected void Page_Load(object sender, EventArgs e)
        {
            db = new RailwayReservationDataContext();
            pMessage.Visible = false;
            cboTrain.ReadOnly = true;
            cboOrigin.ReadOnly = true;
            cboDestination.ReadOnly = true;
            if (!IsPostBack)
            {
                AutoFillTimes();
                if (!String.IsNullOrWhiteSpace(Request.QueryString["id"]))
                {
                    int id;
                    var index = Request.QueryString["id"];
                    if (!Int32.TryParse(index, out id))
                    {
                        Response.Redirect("~/manager/schedule_list.aspx");
                    }
                    var ob = db.Schedules.First(p => p.ScheduleId.Equals(id));
                    if (ob != null)
                    {
                        var train = db.Trains.First(p => p.TrainNo == ob.TrainNo);
                        var origin = db.Stations.First(p => p.StationCode == ob.Origin);
                        var destination = db.Stations.First(p => p.StationCode == ob.Destination);
                        txtScheduleId.Value = ob.ScheduleId.ToString();
                        cboTrain.Text = train.TrainName;
                        cboOrigin.Text = origin.StationName;
                        cboDestination.Text = destination.StationName;
                        txtDepTime.Text = ob.DepTime.ToString();
                        txtArrTime.Text = ob.ArrTime.ToString();
                        txtTravelTime.Text = ob.TravelTime.ToString();
                        txtDistance.Text = ob.Distance.ToString();
                        ParseRunDays(ob.RunDays);
                    }
                }
                else
                {
                    Response.Redirect("~/manager/schedule_list.aspx");
                }
            }
        }

        private void AutoFillTimes()
        {
            if (String.IsNullOrWhiteSpace(txtDepTime.Text)) { txtDepTime.Text = "00:00:00"; }
            if (String.IsNullOrWhiteSpace(txtArrTime.Text)) { txtArrTime.Text = "00:00:00"; }
            if (String.IsNullOrWhiteSpace(txtTravelTime.Text)) { txtTravelTime.Text = "00:00:00"; }
        }

        public string GetRunDays()
        {
            var sb = new StringBuilder();
            for (int i = 0; i < 7; i++)
            {
                if (chlRunDays.Items[i].Selected == true)
                {
                    sb.Append("1");
                }
                else
                {
                    sb.Append("0");
                }
            }
            return sb.ToString();
        }

        public void ParseRunDays(string runDays)
        {
            if (runDays.Length < 7) return;
            var rd = runDays.ToCharArray();
            for (int i = 0; i < 7; i++)
            {
                if (rd[i].Equals('1'))
                {
                    chlRunDays.Items[i].Selected = true;
                }
                else
                {
                    chlRunDays.Items[i].Selected = false;
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (Page.IsValid)
                {
                    AutoFillTimes();
                    var ob = db.Schedules.First(p => p.ScheduleId.Equals(Int32.Parse(txtScheduleId.Value.Trim())));
                    ob.DepTime = TimeSpan.Parse(txtDepTime.Text.Trim());
                    ob.ArrTime = TimeSpan.Parse(txtArrTime.Text.Trim());
                    ob.TravelTime = TimeSpan.Parse(txtTravelTime.Text.Trim());
                    ob.Distance = Double.Parse(txtDistance.Text.Trim());
                    ob.RunDays = GetRunDays();
                    db.SubmitChanges();
                    Response.Redirect("~/manager/schedule_list.aspx");
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        private void ShowMessage(string msg)
        {
            pMessage.Visible = true;
            pMessage.InnerText = msg;
        }
    }
}